package com.example.app.adapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.lifecycle.LiveData;
import androidx.recyclerview.widget.RecyclerView;

import com.example.app.R;
import com.example.app.model.ChatMessage;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class ChatAdapter extends RecyclerView.Adapter<ChatAdapter.MessageViewHolder> {
    private static final String TAG = "ChatAdapter";
    private final LiveData<List<ChatMessage>> messageList;

    public ChatAdapter(LiveData<List<ChatMessage>> messageList) {
        this.messageList = messageList;
    }

    @NonNull
    @Override
    public MessageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        if (viewType == ChatMessage.TYPE_USER) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_message_sent, parent, false);
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_message_received, parent, false);
        }
        return new MessageViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MessageViewHolder holder, int position) {
        List<ChatMessage> messages = messageList.getValue();
        if (messages == null || position >= messages.size()) {
            return;
        }

        ChatMessage message = messages.get(position);
        // Log.d(TAG, "绑定消息 - 位置: " + position + ", 内容: " + message.getContent() + ", 类型: " + message.getType());

        // 设置时间格式为年月日时分秒
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        String timeStr = sdf.format(new Date(message.getTimestamp()));

        holder.messageText.setText(message.getContent());
        holder.timeText.setText(timeStr);
    }

    @Override
    public int getItemCount() {
        List<ChatMessage> messages = messageList.getValue();
        return messages != null ? messages.size() : 0;
    }

    @Override
    public int getItemViewType(int position) {
        List<ChatMessage> messages = messageList.getValue();
        if (messages == null || position >= messages.size()) {
            return ChatMessage.TYPE_USER;
        }
        return messages.get(position).getType();
    }

    static class MessageViewHolder extends RecyclerView.ViewHolder {
        LinearLayout leftLayout;
        LinearLayout rightLayout;
        TextView messageText;
        TextView timeText;

        MessageViewHolder(View itemView) {
            super(itemView);
            leftLayout = itemView.findViewById(R.id.left_layout);
            rightLayout = itemView.findViewById(R.id.right_layout);
            messageText = itemView.findViewById(R.id.message_text);
            timeText = itemView.findViewById(R.id.tv_time);
        }
    }
} 